﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="Button.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2011 
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.GUI.
//
//   It is released under the Microsoft Public License (Ms-PL).
//
//   You should have received a copy of the Microsoft Public License
//   along with starLiGHT.GUI. If not, see http://sle.codeplex.com/license.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.GUI are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev::                       $:  Revision of last commit *
// * $Author::                    $:  Author of last commit   *
// * $Date::                      $:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.GUI.Widgets
{
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion

    public class Button : ButtonBase
    {
        private bool mouseWasDown = false;

        #region Constructors
        public Button(GuiManager manager, string name)
            : base(manager, name)
        { 
        }

        public Button(GuiManager manager, string name, string layout)  
            : base(manager, name, layout)
        {
        }
        #endregion

        public override bool CanFocus
        {
            get
            {
                return true;
            }
        }

        public override void DrawText(IGuiRenderer renderer, Layer layer, TextLabel te)
        {
            if (te.Name.Equals("main", StringComparison.InvariantCultureIgnoreCase))
            {
                renderer.DrawString(Font, Text, GetTextPosition(Text, TextAlignment, 0, 0), ForeColor);
            }
        }

        public override void OnMouseDown(object sender, MouseEventArgs e)
        {
            if (!e.WidgetUnderMouse)
            {
                return;
            }

            base.OnMouseDown(this, e);

            SetState("Pushed");
            this.mouseWasDown = true;
        }

        public override void OnMouseUp(object sender, MouseEventArgs e)
        {
            if (this.mouseWasDown && e.WidgetUnderMouse)
            {
                base.OnMouseUp(this, e);
            }

            if (e.WidgetUnderMouse)
            {
                SetState("Hover");
            }
            else
            {
                SetState("Normal");
            }

            this.mouseWasDown = false;
        }

        public override void OnMouseLeave(object sender, EventArgs e)
        {
            base.OnMouseLeave(this, e);

            if (this.mouseWasDown)
            {
                SetState("Hover");
            }
            else
            {
                SetState("Normal");
            }
        }

        public override void OnMouseEnter(object sender, MouseEventArgs e)
        {
            base.OnMouseEnter(sender, e);

            if (e.LeftButton && this.mouseWasDown)
            {
                SetState("Pushed");
            }
            else
            {
                SetState("Hover");
            }
        }
    }
}
